﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Gw2MumbleLinkServer
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            bool silent = false;
            ushort port = Server.DEFAULT_PORT;

            for (int i = 0, length = args.Length; i < length; i++)
            {
                switch (args[i].ToLower())
                {
                    case "-port":
                        if (i + 1 < length)
                        {
                            if (UInt16.TryParse(args[i + 1], out port))
                                i++;
                        }
                        break;
                    case "-silent":
                        silent = true;
                        break;
                }
            }

            if (!silent)
            {
                Application.EnableVisualStyles();
                using (formMain form = new formMain(port))
                {
                    if (form.ShowDialog() != DialogResult.OK)
                        return;
                    port = form.Port;
                }
            }

            Server server = null;
            try
            {
                server = new Server(port);
            }
            catch (Exception e)
            {
                MessageBox.Show("Unable to initialize MumbleLink.\n\n" + e.Message, "An error has occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                server.Start();
            }
            catch (Exception e)
            {
                MessageBox.Show("Unable to start the server on 127.0.0.1:" + server.Port + ".\n\n" + e.Message, "An error has occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
    }
}
